//
//  HDTabBarVC.m
//  HDHuNanMuseum
//
//  Created by liuyi on 2017/9/2.
//  Copyright © 2017年 liuyi. All rights reserved.
//

#import "HDTabBarVC.h"
#import "HDRootAVC.h"
#import "HDRootBVC.h"
#import "HDRootCVC.h"
#import "HDRootDVC.h"
#import "HDRootEVC.h"
#import "LLTabBar.h"
#import "AppDelegate.h"
#import "HD_NKM_Map_Obj.h"
//#import "HDLY_AutoLogin.h"
#import "HD_NKM_Map_Size_Obj.h"

@interface HDTabBarVC ()

@end

@implementation HDTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadControllers];
    [self naviBtnInit];
    // 自动登录
//    [HDLY_AutoLogin autoLoginAction];
//    [HDLY_AutoLogin machineIDRequestAction];
    
    [self allMapInfoRequestAction];
    
    //上传收号，定位
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(uploadNowLocation:)
                                                 name:iBeacon_Send_Notification
                                               object:nil];
}

- (void)naviBtnInit
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0.0, 0.0, 45, 45);
    
    [leftButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    leftButton.hidden = YES;
    UIBarButtonItem *leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:leftButton];
    [self.navigationItem setLeftBarButtonItem:leftBarButtonItem];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar"]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[UIImage new]];
    
    navigationBar.tintColor = [UIColor whiteColor];
    NSMutableDictionary *attributes= [[NSMutableDictionary alloc] init];
    [attributes setValue:[UIColor whiteColor] forKey:NSBackgroundColorAttributeName];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    self.navigationController.navigationBarHidden = NO;
}

//收号上传位置
- (void)uploadNowLocation:(NSNotification *)noti
{
    NSNumber *locationNum = noti.object;
//    NSString *deviceID = [[HDDeclare sharedDeclare] getUserPhoneID];
    NSString *deviceID = [[HDDeclare sharedDeclare] deviceno];
    if (deviceID == nil)
        return;
    
    if (locationNum.integerValue > 0 && deviceID != nil) {
        NSString *requestStr = [NSString stringWithFormat:@"%@/positions",[HDDeclare IP_Request_Header]];
        NSDictionary *params = @{@"p":@"i",
                                 @"deviceno":deviceID,
                                 @"auto_num":locationNum};
        [HD_LY_NetHelper requestWithType:POST
                                 withUrl:requestStr
                              withParams:params
                                toShowVC:self
                                 success:^(id response) {
                                     
                                 }
                                    fail:^(NSError *error) {
                                        LOG(@"Upload error：%@", error);
                                    }
                                 showHUD:NO
                                 isCache:NO];
    }
}
#pragma mark---------- 获取全部的地图信息
- (void)allMapInfoRequestAction
{
    NSString *requestStr = [NSString stringWithFormat:@"%@/map_info",[HDDeclare IP_Request_Header]];
    
    [HD_LY_NetHelper requestWithType:GET
                             withUrl:requestStr
                          withParams:@{@"p":@"i"}
                            toShowVC:nil
                             success:^(id response) {
                                 NSDictionary *dic = response;
                                 NSLog(@"地图尺寸%@",dic);
                                 if ([dic isKindOfClass:[NSDictionary class]]) {
                                     NSNumber *status = dic[@"status"];
                                     if (status.integerValue == 1) {
                                         NSArray *dataArray = dic[@"data"];
                                         [self addMapSizeObjWith:dataArray];
                                     }
                                 }
                             }
                                fail:^(NSError *error) {
                                    LOG(@"machine error：%@", error);
                                }
                             showHUD:NO
                             isCache:NO];
}
- (void)addMapSizeObjWith:(NSArray *)dataArray
{
    NSMutableArray *sizeArray = [NSMutableArray new];
    for (NSInteger i=0; i<dataArray.count; i++) {
        
        NSDictionary *sizeDic = dataArray[i];
        HD_NKM_Map_Size_Obj *sizeObj = [HD_NKM_Map_Size_Obj mapSizeWithDict:sizeDic];
        [sizeArray addObject:sizeObj];
    }
    
    [[HDDeclare sharedDeclare] setMapSizeArray:sizeArray];
}

- (void)loadControllers {
    //创建tabBarController
    HDRootAVC *vcA = [[UIStoryboard storyboardWithName:@"RootA" bundle:nil] instantiateViewControllerWithIdentifier:@"HDRootAVC"];
    HDRootBVC *vcB = [[UIStoryboard storyboardWithName:@"RootB" bundle:nil] instantiateViewControllerWithIdentifier:@"HDRootBVC"];
    HDRootCVC *vcC = [[UIStoryboard storyboardWithName:@"RootC" bundle:nil] instantiateViewControllerWithIdentifier:@"HDRootCVC"];
    HDRootDVC *vcD = [[UIStoryboard storyboardWithName:@"RootD" bundle:nil] instantiateViewControllerWithIdentifier:@"HDRootDVC"];
    HDRootEVC *vcE = [[UIStoryboard storyboardWithName:@"RootE" bundle:nil] instantiateViewControllerWithIdentifier:@"HDRootEVC"];
    
    UIColor *bar_tint = RGB(237, 237, 237);
    UINavigationController * nav1 = [[UINavigationController alloc]initWithRootViewController:vcA];
    [nav1.navigationBar setBarTintColor:bar_tint];
    UINavigationController * nav2 = [[UINavigationController alloc]initWithRootViewController:vcB];
    [nav2.navigationBar setBarTintColor:bar_tint];
    UINavigationController * nav3 = [[UINavigationController alloc]initWithRootViewController:vcC];
    [nav3.navigationBar setBarTintColor:bar_tint];
    UINavigationController * nav4 = [[UINavigationController alloc]initWithRootViewController:vcD];
    [nav4.navigationBar setBarTintColor:bar_tint];
    UINavigationController * nav5 = [[UINavigationController alloc]initWithRootViewController:vcE];
    [nav5.navigationBar setBarTintColor:bar_tint];

    //添加控制器
    self.viewControllers = @[nav1,nav2,nav3,nav4,nav5];
    
    [self setupTabbar];
    
    //获取全部POI资源
    [self allResRequestAction];
//    //开始收号
//    [[HD_NKM_Mediator shareInstance] startRangingAllRegions]; // 打开蓝牙
}

- (void)setupTabbar {
    LLTabBar *tabBar = [[LLTabBar alloc] initWithFrame:self.tabBar.bounds];
    tabBar.tabBarItemAttributes = @[@{kLLTabBarItemAttributeTitle : @"首页", kLLTabBarItemAttributeNormalImageName : @"home_normal", kLLTabBarItemAttributeSelectedImageName : @"home_highlight", kLLTabBarItemAttributeType : @(LLTabBarItemNormal)},
                                    @{kLLTabBarItemAttributeTitle : @"服务", kLLTabBarItemAttributeNormalImageName : @"service_normal", kLLTabBarItemAttributeSelectedImageName : @"service_highlight", kLLTabBarItemAttributeType : @(LLTabBarItemNormal)},
                                    @{kLLTabBarItemAttributeTitle : @"导览", kLLTabBarItemAttributeNormalImageName : @"guide_normal", kLLTabBarItemAttributeSelectedImageName : @"guide_highlight", kLLTabBarItemAttributeType : @(LLTabBarItemRise)},
                                    @{kLLTabBarItemAttributeTitle : @"教育", kLLTabBarItemAttributeNormalImageName : @"edu_normal", kLLTabBarItemAttributeSelectedImageName : @"edu_highlight", kLLTabBarItemAttributeType : @(LLTabBarItemNormal)},
                                    @{kLLTabBarItemAttributeTitle : @"个人", kLLTabBarItemAttributeNormalImageName : @"account_normal", kLLTabBarItemAttributeSelectedImageName : @"account_highlight", kLLTabBarItemAttributeType : @(LLTabBarItemNormal)}];
    
    [self.tabBar addSubview:tabBar];
    self.selectedIndex = 0;
    self.tabBar.backgroundImage = [[UIImage alloc]init];
    self.tabBar.shadowImage = [[UIImage alloc]init];
    AppDelegate *dele = kAppDelegate;
    dele.tabBarVC = self;
}

#pragma mark -------- 获取全部POI资源
// 获取全部的展品
- (void)allResRequestAction
{
    NSString *requestStr = [NSString stringWithFormat:@"%@/map_exhibit",[HDDeclare IP_Request_Header]];
    NSDictionary *paraDic = @{@"p":@"i",
                              @"language":[[HDDeclare sharedDeclare] nowLanguageIndexStr]};
    
    [HD_LY_NetHelper requestWithType:GET
                             withUrl:requestStr
                          withParams:paraDic
                            toShowVC:nil
                             success:^(id response) {
                                 NSDictionary *dic = response;
                                 if ([dic isKindOfClass:[NSDictionary class]]) {
                                     NSNumber *status = dic[@"status"];
                                     NSArray *array = dic[@"data"];
                                     
                                     NSMutableArray *poiArray = [NSMutableArray new];
                                     if (status.integerValue == 1 && [array isKindOfClass:[NSArray class]]) {
                                         
                                         for (NSInteger i=0;i<array.count;i++) {
                                             NSDictionary *itemDic = array[i];
                                             HD_NKM_Map_Obj *obj = [HD_NKM_Map_Obj mapPoiWithDict:itemDic];
                                             [poiArray addObject:obj];
                                         }
                                         [[HDDeclare sharedDeclare] setResArray:poiArray];
                                     }
                                 }
                             }
                                fail:^(NSError *error) {
                                    LOG(@"Route exihibit error：%@", error);
                                }
                             showHUD:NO
                             isCache:NO];
}



@end
